import { ref } from 'vue'
import pageDialog from '@/components/page-dialog'

type CallBackFnType = (item?: any) => void

export function usePageDialog(createCallBack?: CallBackFnType, editCallBack?: CallBackFnType) {
  const editInfo = ref({})
  const pageDialogRef = ref<InstanceType<typeof pageDialog>>()

  const handleCreateData = () => {
    editInfo.value = {}
    if (pageDialogRef.value) {
      pageDialogRef.value.dialogVisible = true
    }
    createCallBack && createCallBack()
  }

  const handleEditData = (item: any) => {
    editInfo.value = { ...item }
    if (pageDialogRef.value) {
      pageDialogRef.value.dialogVisible = true
    }
    editCallBack && editCallBack(item)
  }

  return {
    editInfo,
    pageDialogRef,
    handleCreateData,
    handleEditData
  }
}
